"""
<name>Crossover</name>
<description>Crossover Genetic Operator</description>
<contact>Leidy Garzon</contact>
<icon>icons/Xover.svg</icon>
<priority>1080</priority>

"""
from Goldenberry.widgets import *
from Goldenberry.widgets import binary_xover_functions
from Goldenberry.widgets import real_xover_functions
from Goldenberry.widgets import OWWidget, inspect, load_widget_ui, OWGUI, QObject, QApplication, QClipboard, QtCore

class GbCrossoverWidget(OWWidget):
    """Crossover method"""
    
    #attributes
    solution_domain_index = 0
    solution_domains = []
    crosser_index = [0]
    crossers = {}      

    def __init__(self, parent=None, signalManager=None):
        OWWidget.__init__(self, parent, signalManager, 'Genetic crossover operators')
        self.solution_domains = ["Binary","Real"]
        self.crossers = self.__setup_crossover_list()
        self.setup_interfaces()
        self.setup_ui() 

    def setup_interfaces(self):
        self.outputs = [("Crosser", Crosser)]
        
    def setup_ui(self):
        # Loads the UI from an .ui file.
        load_widget_ui(self)
        #solution_domain_comboBox = OWGUI.comboBox(self.paramBox, self, "solution_domain_index", label="Select the genetic mutation operator", items=self.solution_domains, tooltip="Select the domain of the solution", callback = self.set_solution_domain)
        solution_domain_comboBox = OWGUI.comboBox(self.paramBox, self, "solution_domain_index", items=self.solution_domains, tooltip="Select the domain of the solution", callback = self.set_solution_domain)
        crosser_listbox = OWGUI.listBox(self.paramBox, self, "crosser_index", "crossers", tooltip="Select the genetic crossover operator", callback = self.set_crossover)
       # OWGUI.separator(self.paramBox)
        #a= OWGUI.QLabel('Select the genetic mutation operator',self.paramBox)
        #set new binding controls
        #selection_combobox = OWGUI.comboBox(self, self, "mutation_index", items=self.binary_mutations, tooltip="Select the parents selection method")
        #self.paramBox.setLayout(QFormLayout(self.paramBox))
        #self.paramBox.layout().addRow(OWGUI.QLabel('Select the genetic mutation operator')) 
        #self.paramBox.layout().addRow("Solution Domine" ,solution_domain_comboBox)
        #self.paramBox.layout().addRow(OWGUI.QLabel('Select the genetic mutation operator')) 
        #self.paramBox.layout().addRow(mutation_listbox)

        # Subscribe to signals
        QObject.connect(self.buttonBox,QtCore.SIGNAL("accepted()"), self.accepted)
        QObject.connect(self.buttonBox,QtCore.SIGNAL("rejected()"), self.rejected)
        QObject.connect(self.copy_button,QtCore.SIGNAL("clicked()"), self.copy)
        QObject.connect(self.paste_button,QtCore.SIGNAL("clicked()"), self.paste)

    def copy(self):
        QApplication.clipboard().setText(self.crossover_text.toPlainText(), QClipboard.Clipboard)
    
    def paste(self):
        self.custom_text.setText(QApplication.clipboard().text())
    
    def accepted(self):
        self.__set_crosser()
        self.accept()
        self.send("Crosser", self.crosser)

    def rejected(self):
        self.reject()

    def set_solution_domain(self):
        self.crossers = self.__setup_crossover_list(self.solution_domain_index)

    def set_crossover(self):
        pass
        """if self.encod_index[0] == 2:
            self.box.setEnabled(True)
        else:
            self.box.setEnabled(False)"""
    def __set_crosser(self):
        self.crosser = Crosser()
        self.crosser.set_crossover_method(self.crossers.values()[self.crosser_index[0]].__name__)
                        
    def __setup_crossover_list(self, domain = 0):
        if domain == 1:
            crossover_list = dict(inspect.getmembers(real_xover_functions, lambda member: inspect.isclass(member)))
        else:
            crossover_list = dict(inspect.getmembers(binary_xover_functions, lambda member: inspect.isclass(member)))
        del crossover_list['CrossoverStrategy']
        return crossover_list


if __name__=="__main__":
    test_widget()

def test_widget():
    appl = QApplication(sys.argv)
    ow = GbCrossoverWidget()
    ow.show()
    appl.exec_()